﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using QuanLyThuPhiTaiLieu.Common;
using QuanLyThuPhiTaiLieu.Entity;
using QuanLyThuPhiTaiLieu.Model;

namespace QuanLyThuPhiTaiLieu.Biz
{
    public class DanhMucBiz : BaseBiz
    {
        public int SaveItem(DanhMuc item)
        {
            var dbContext = new Context();

            dbContext.DanhMucs.Add(item);
            int newId = dbContext.SaveChanges();

            return newId;
        }

        public void UpdateItem(DanhMuc item)
        {
            var dbContext = new Context();
            dbContext.Entry(item).State = EntityState.Modified;
            dbContext.SaveChanges();
        }

        public List<DanhMuc> ViewAll()
        {
            var dbContext = new Context();
            return (from item in dbContext.DanhMucs
                    orderby item.Sqb ascending
                    select item).ToList();
        }

        public DanhMuc LoadItem(int id)
        {
            var dbContext = new Context();
            return (from item in dbContext.DanhMucs
                    where item.Id == id
                    select item).First();
        }

        public DanhMuc LoadItem(string khlt)
        {
            var dbContext = new Context();
            var items = (from item in dbContext.DanhMucs
                        where item.Khlt == khlt
                        select item).ToList();

            if (items.Count > 0)
            {
                return items.First();
            }
            return null;
        }

        public void RemoveItem(List<DanhMuc> items)
        {
            var dbContext = new Context();
            foreach (var tailieu in items)
            {
                dbContext.DanhMucs.Attach(tailieu);
                dbContext.DanhMucs.Remove(tailieu);
                dbContext.Entry(tailieu).State = EntityState.Deleted;
            }

            dbContext.SaveChanges();
        }
    }
}
